const Koa = require('koa')
const cors = require('@koa/cors');
const bodyParser = require('koa-bodyparser')
// const userRouter = require('../router/user.router')
// const authRouter = require('../router/auth.router')
const errorHandler = require('./error-handle')
const useRouters = require('../router/index')
const app = new Koa()

app.use(bodyParser())
app.use(cors());

//原生解决跨域问题
// app.use(async (ctx, next)=> {
//   ctx.set('Access-Control-Allow-Origin', '*');
//   ctx.set('Access-Control-Allow-Headers', 'Content-Type, Content-Length, Authorization, Accept, X-Requested-With , yourHeaderFeild');
//   ctx.set('Access-Control-Allow-Methods', 'PUT, POST, GET, DELETE, OPTIONS');
//   if (ctx.method == 'OPTIONS') {
//     ctx.body = 200; 
//   } else {
//     await next();
//   }
// });


// app.use(userRouter.routes());
// app.use(userRouter.allowedMethods())
// app.use(authRouter.routes());
// app.use(authRouter.allowedMethods())

// useRouters(app)

app.useRouters = useRouters
app.useRouters()


app.on('error',errorHandler)
module.exports = app